package com.shanzmoo.admin.web.sys;

import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.core.domain.UserSaveDto;
import com.shanzmoo.core.domain.UserUpdateDto;
import com.shanzmoo.admin.manager.SysUserManager;
import com.shanzmoo.db.sys.entity.dto.*;
import com.shanzmoo.db.sys.entity.vo.UserPageVo;
import com.shanzmoo.db.sys.entity.vo.UserPermissionVo;
import com.shanzmoo.db.sys.entity.vo.UserSelectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Gzhao
 * @since 2020-11-06
 */
@Api(tags = "系统-用户管理")
@RestController
@RequestMapping("/api/sys/user")
public class UserController {

    @Autowired
    private SysUserManager sysUserManager;


    @ApiOperation(value = "门店新增")
    @PostMapping("/outlet/add")
    public SHttpResponse<Boolean> outletAdd(@Valid @RequestBody UserOutletSaveDto saveDto) {
        SResponse<Boolean> re = sysUserManager.outletAdd(saveDto);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "系统新增")
    @PostMapping("/add")
    public SHttpResponse<Boolean> add(@Valid @RequestBody UserSaveDto saveDto) {
        SResponse<Boolean> re = sysUserManager.add(saveDto);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "删除")
    @GetMapping("/delete")
    public SHttpResponse<Boolean> deleteById(@RequestParam(value = "userId") Integer userId) {
        SResponse<Boolean> re = sysUserManager.deleteById(userId);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "修改")
    @PostMapping("/update")
    public SHttpResponse<Boolean> update(@Valid @RequestBody UserUpdateDto updateDto) {
        SResponse<Boolean> re = sysUserManager.edit(updateDto);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "禁用用户")
    @GetMapping("/disable")
    public SHttpResponse<Boolean> disableById(@RequestParam(value = "userId") Integer userId) {
        SResponse<Boolean> re = sysUserManager.changeStatusById(userId, false);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "启用用户")
    @GetMapping("/active")
    public SHttpResponse<Boolean> activeById(@RequestParam(value = "userId") Integer userId) {
        SResponse<Boolean> re = sysUserManager.changeStatusById(userId, true);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "分页列表")
    @PostMapping("/pageList")
    public SHttpResponse<PageRespBase<UserPageVo>> pageList(@Valid @RequestBody UserQueryDto queryDto) {
        SResponse<PageRespBase<UserPageVo>> re = sysUserManager.getPageList(queryDto);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "权限列表")
    @GetMapping("/permissionInfo")
    public SHttpResponse<UserPermissionVo> getPermissionInfo() {
        SResponse<UserPermissionVo> re = sysUserManager.getPermissionInfo();
        return SHttpResponse.transform(re);
    }


    @ApiOperation(value = "门店用户列表")
    @PostMapping("/outlet/pageList")
    public SHttpResponse<PageRespBase<UserPageVo>> outletPageList(@Valid @RequestBody UserQueryDto queryDto) {
        SResponse<PageRespBase<UserPageVo>> re = sysUserManager.getOutletPageList(queryDto);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "重置密码")
    @GetMapping("/initPasswrod")
    public SHttpResponse<Boolean> initPasswrod(@RequestParam(value = "userId") Integer userId) {
        SResponse<Boolean> re = sysUserManager.initPasswrod(userId);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "修改个人密码")
    @PostMapping("/udpatePassword")
    public SHttpResponse<Boolean> udpatePassword(@Valid @RequestBody UserUpdatePasswordDto updateDto) {
        SResponse<Boolean> re = sysUserManager.udpatePassword(updateDto);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "根据指定权限获取用户信息列表")
    @GetMapping("/getUserListByRoleId")
    public SHttpResponse<List<UserSelectVo>> getUserListByRoleId(@RequestParam(value = "roleId") Integer roleId) {
        SResponse<List<UserSelectVo>> re = sysUserManager.getUserListByRoleId(roleId);
        return SHttpResponse.transform(re);
    }

}
